package com.entry.vmsg.src;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.req.vmsg.processor.VMsgProcessor;
import com.req.vmsg.util.TxtWebConstants;
import com.req.vmsg.util.VMsgUtil;
import com.util.vmsg.beans.TxtWebMessage;
import com.util.vmsg.beans.TxtWebResponse;

@SuppressWarnings("serial")
public class VMsgHandler extends HttpServlet {
	private static final Logger log = Logger.getLogger(VMsgHandler.class.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		TxtWebMessage txtWebMessage = new TxtWebMessage();
		
		// Retrieve message information
		txtWebMessage.applyRequestValues(req);

		// TODO: Implement request verification from txt Web
		//txtWebMessage.verifyRequest();
		
		txtWebMessage = VMsgUtil.validateRequestData(txtWebMessage);
		
		VMsgProcessor vmsnger = new VMsgProcessor();
		if(txtWebMessage.isDataValid())
		{
			if (txtWebMessage.getRequestType().equals(TxtWebConstants.RequestTypes.REGISTER))
			{
				TxtWebResponse txtWebResponse = vmsnger.processRegistration(txtWebMessage);
				//vmsnger.sendVMsg(txtWebResponse);
				String htmlMessage = formatResponseToHTML(txtWebResponse.getMessage());
				sendResponse(resp, htmlMessage);
			}
			
			if (txtWebMessage.getRequestType().equals(TxtWebConstants.RequestTypes.DEREGISTER))
			{
				vmsnger.processDeregistration(txtWebMessage);
			}
			
			if (txtWebMessage.getRequestType().equals(TxtWebConstants.RequestTypes.RELAY_MESSAGE))
			{
				try {
					RequestDispatcher rd = req.getRequestDispatcher("jsp/DisplayData.jsp");
					rd.include(req, resp);
				} catch (ServletException e) {
					log.info("Issue while including the request and resposne objects...");
					e.printStackTrace();
				}
				vmsnger.forwardVMsg(txtWebMessage);
			}
			
			if(txtWebMessage.getRequestType().equals(TxtWebConstants.RequestTypes.HELP))
			{
				TxtWebResponse txtWebResponse = new TxtWebResponse();
				txtWebResponse.setMessage(txtWebMessage.getHelpMessage());
				txtWebResponse.setMobileId(txtWebMessage.getTxtWebMobileNumber());
				//vmsnger.sendVMsg(txtWebResponse);
				String htmlMessage = formatResponseToHTML(txtWebResponse.getMessage());
				sendResponse(resp, htmlMessage);
			}
		}
		else
		{
			TxtWebResponse txtWebResponse = new TxtWebResponse();
			txtWebResponse.setMessage(TxtWebConstants.VMsgHelpMessages.ERROR + TxtWebConstants.VMsgHelpMessages.APP_DEFAULT_INFO);
			txtWebResponse.setMobileId(txtWebMessage.getTxtWebMobileNumber());
			//vmsnger.sendVMsg(txtWebResponse);
			String htmlMessage = formatResponseToHTML(txtWebResponse.getMessage());
			sendResponse(resp, htmlMessage);
		}
		
	}
	
	private String formatResponseToHTML(String responseString) {
			return ("<html><head><meta name=\"txtweb-appkey\" content=\"557bad70-7ac2-4f97-aa47-3fb520b78601\"/></head><body>"
					+ responseString
					+"</body></html>"); 
	}
	
	private void sendResponse(HttpServletResponse resp, String htmlMessage)
	{
		try {
			PrintWriter out = resp.getWriter();
			out.println(htmlMessage); 
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}